package helper;

import javax.mail.*;
import javax.mail.internet.*;

import databeans.Customer;
import databeans.Employee;
import databeans.Fund;
import databeans.Transaction;

import java.util.*;

/**
 * A simple email sender class.
 */
public class Postman {
    
    public static final String smtpServer = "smtp.gmail.com";
    public static final String from = "majesticCFAnoreply@gmail.com";
    
    private String adminEmail = "alesmana@gmail.com";
    
    public static void sendCreateEmployeeConfirmation(Employee employee, String password){
        String subject = "Good news! Your CFA Employee account has been created";
        String body = "Hi " + employee.getFirstName() + "!\n\n"+
        "Your account has been created with these following information \n" + 
        "USERID: " + employee.getUsername() + "\n" +
        "PASSWORD: " + password + "\n" +
        "Do not forget to change your password :) \n\n" +
        "CFA IT Team"; 
        
        send (employee.getEmail(),  subject, body);
    }
    
    public static void sendCreateCustomerConfirmation(Customer customer, String password){
        String subject = "Good news! Your CFA Customer account has been created";
        String body = "Hi " + customer.getFirstName() + "!\n\n"+
        "Your account has been created with these following information \n" + 
        "USERID: " + customer.getUsername() + "\n" +
        "PASSWORD: " + password + "\n" +
        "Do not forget to change your password :) \n\n" +
        "CFI IT Team"; 
        
        send (customer.getEmail(), subject, body);
    }
    
    public static void sendResetCustomerPasswordConfirmation(Customer customer, String password){
        String subject = "Good news! Your CFA Customer password has been resetted";
        String body = "Hi " + customer.getFirstName() + "!\n\n"+
        "Your new login information is as followed \n" + 
        "USERID: " + customer.getUsername() + "\n" +
        "PASSWORD: " + password + "\n" +
        "Do not forget to change your password :) \n\n" +
        "CFI IT Team"; 
        
        send (customer.getEmail(), subject, body);
    }

    public static void sendBuyOrderConfirmation(Transaction transaction, Fund fund, Customer customer){
        String subject = "Buy Order Confirmation";
        String body = "Hi " + customer.getFirstName() + "!\n\n"+
        "Your Buy Order has been put with these following information \n" + 
        "TRANSACTION ID: " + transaction.getTransactionID() + "\n"+
        "FUND: #" + fund.getFundID()+ " "+ fund.getName() + "(" + fund.getSymbol()+ ") \n" +
        "AMOUNT: $" + transaction.getAmount() + "\n" +
        "Please keep this email as receipt :) \n\n" +
        "CFA IT Team"; 
        
        send (customer.getEmail(),  subject, body);
    }
    
    
    public static void send (String to, String subject, String body){
        Postman.send(smtpServer, to, from, subject, body);
    }

    public static void send(String smtpServerx, String tox, String fromx,
            String subjectx, String bodyx) {
        try {
            
            String to = tox;
            String from = fromx ;   
            String host = smtpServerx;
            String subject = subjectx;
            String body = bodyx;
            
            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

            // Create properties, get Session
            Properties props = new Properties();

            // If using static Transport.send(),
            // need to specify which host to send it to
            props.put("mail.smtp.host", host);
            
            // To see what is going on behind the scene
            //props.put("mail.debug", "true");
            
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.starttls.enable","true");
            
            props.put("mail.smtp.auth", "true");
            
            Authenticator auth = new SMTPAuthenticator();

            Session session = Session.getInstance(props, auth);
            
            boolean debug = false;
            session.setDebug(debug);

            try {

                // create a message
                Message msg = new MimeMessage(session);

                // set the from and to address
                InternetAddress addressFrom = new InternetAddress(from);
                msg.setFrom(addressFrom);
                
                String [] recipients = { to };
                InternetAddress[] addressTo = new InternetAddress[recipients.length];
                for (int i = 0; i < recipients.length; i++) {
                    addressTo[i] = new InternetAddress(recipients[i]);
                }
                msg.setRecipients(Message.RecipientType.TO, addressTo);
                
                msg.setSubject(subject);
                msg.setSentDate(new Date());

                // Set message content
                msg.setText(body);

                //Send the message
                Transport.send(msg);
            }
            catch (MessagingException mex) {
                // Prints all nested (chained) exceptions as well
                mex.printStackTrace();
            }
            
            
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    

}
